package com.android.support.lib.universal.util;

import android.graphics.Bitmap;
import android.graphics.Matrix;

/**
 * 图片压缩 使用说明：
 * UtilImageThumbnail imageThumbnail=new UtilImageThumbnail();
 * int scale =imageThumbnail.reckonThumbnail(bm.getWidth(),bm.getHeight(), 500, 600);
 * bm = imageThumbnail.PicZoom(bm, bm.getWidth() / scale,bm.getHeight() /scale);
 * 
 * @author 彭治铭
 *
 */
public class UtilimageThumbnail {
	/**
	 * 获取压缩比例
	 * 
	 * @param oldWidth
	 * @param oldHeight
	 * @param newWidth
	 * @param newHeight
	 * @return
	 */
	public int reckonThumbnail(int oldWidth, int oldHeight, int newWidth,
			int newHeight) {
		if ((oldHeight > newHeight && oldWidth > newWidth)
				|| (oldHeight <= newHeight && oldWidth > newWidth)) {
			int be = (int) (oldWidth / (float) newWidth);
			if (be <= 1)
				be = 1;
			return be;
		} else if (oldHeight > newHeight && oldWidth <= newWidth) {
			int be = (int) (oldHeight / (float) newHeight);
			if (be <= 1)
				be = 1;
			return be;
		}
		return 1;
	}

	/**
	 * 获取压缩后的位图
	 * 
	 * @param bmp
	 * @param width
	 *            压缩后的宽带
	 * @param height
	 *            压缩后高度
	 * @return
	 */
	public Bitmap PicZoom(Bitmap bmp, int width, int height) {
		int bmpWidth = bmp.getWidth();
		int bmpHeght = bmp.getHeight();
		Matrix matrix = new Matrix();
		matrix.postScale((float) width / bmpWidth, (float) height / bmpHeght);
		return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeght, matrix, true);
	}

}
